package cn.comon.app.scan.service.analysis.impl;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.comon.app.scan.component.SysConfigBean;
import cn.comon.app.scan.entity.AsReportinfo;
import cn.comon.app.scan.service.AsReportinfoService;
import cn.comon.app.scan.service.impl.ThreadPoolService;

/**
 * 第三方软件检测启动类
 * @author xujf
 *
 */
@Service
public class AppAnalysisScheduler {
	
    @Autowired
    private AsReportinfoService asReportinfoService;
    Logger logger = Logger.getLogger(AppAnalysisScheduler.class.getName());
    /**
     * 分析者
     */
    private static String[] analystors;
    
    public AppAnalysisScheduler () {
    	analystors = SysConfigBean.getProperty("app.analystors").split(",");
    }
    public void startAppAnalysis(){
        try {
            logger.info("启动第三方APP分析任务...Start");
            // 创建线程池
            ThreadPoolExecutor executor = ThreadPoolService.getTheadPool("analysisThreadPool", analystors.length);
            
            logger.info("第三方APP分析任务线程池中等待队列中的线程数：" + executor.getQueue().size());
            logger.info("第三方APP分析任务线程池中的线程数：" + executor.getPoolSize());
            logger.info("第三方APP分析任务线程池正在执行的线程数：" + executor.getActiveCount());
            if (executor.getActiveCount() == 0) {
                //检索数据表【as_reportinfo】，取得需要下载的appURL
                List<AsReportinfo> appDataList = asReportinfoService.getReportinfoMaxSize(
                        Integer.parseInt(SysConfigBean.getProperty("select.maxsize")));
              if (appDataList != null && appDataList.size() > 0) {
            	  for (String analystor: analystors) {
            		  executor.execute(new AnalysisProccessImpl(SysConfigBean.getSysConfigFile(), appDataList, analystor));
            	  }
            } else {
              logger.info("无符合条件数据，退出");
            }
            logger.info("完成第三方APP分析任务...End");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
         }
    }
}